<template>
  <div class="list">
    <!-- 面包屑导航 -->
    <breadcrumb firstTitle="商品管理" secondTitle="商品列表"></breadcrumb>

    <el-card>
      <el-row :gutter="22">
        <el-col :span="8">
          <!-- 左侧输入框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品列表 -->
        <el-col :span="4">
          <el-button type="primary" @click="addShopping">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表 -->
      <el-table :data="goodsList" stripe style="width: 100%" border>
        <!-- 索引列 -->
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="95">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button type="primary" @click="editorGoods(scope.row)" size="mini" icon="el-icon-edit"></el-button>

            <!-- 删除 -->
            <el-button type="danger" @click="removeGoodsById(scope.row)" size="mini" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

      <!-- 编辑dialog对话框 -->
      <el-dialog title="编辑商品列表" :visible.sync="editorDialogVisible" width="50%">
        <span>123</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editorDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editorDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 删除dialog对话框 -->
      <el-dialog title="提示" :visible.sync="removeDialogVisible" width="50%">
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import request from '../../network/request'
import breadcrumb from '../breadcurmb/BreakCrumb.vue'

export default {
  name: 'MyList',
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      // 存放商品列表数据
      goodsList: [],
      // 页面总共有多少条数据
      total: 0,
      // 获取商品列表 发起请求参数
      queryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示多少条 数据
        pagesize: 10
      },
      // 控制编辑对话框的显示与隐藏
      editorDialogVisible: false,
      // 控制删除对话框的显示与隐藏
      removeDialogVisible: false
    }
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await request('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表数据失败')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 编辑商品
    editorGoods() {
      this.editorDialogVisible = true
    },
    // 改变当前页面数的范围 触发
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      // 重新获取页面数据
      this.getGoodsList()
    },
    // 改变当前页面页码数时触发
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      // 重新获取页面数据
      this.getGoodsList()
    },
    // 根据商品id删除商品
    async removeGoodsById(row) {
      const resultConfirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (resultConfirm !== 'confirm') return
      const { data: res } = await request.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')

      // 重新渲染商品页面
      this.getGoodsList()
    },
    // 点击添加商品按钮
    addShopping() {
      this.$router.push('/goods/add')
    }
  },
  components: {
    breadcrumb
  }
}
</script>

<style lang="less" scoped>
</style>